const router = require("koa-router")();
const JWT = require("jsonwebtoken");
let user = [
  {
    username: "admin",
    password: "admin",
  },
  {
    username: "user",
    password: "user",
  },
];
let list=[];
let books=['你好','您有什么问题',"再见",'今天天气不错','哈哈哈']



router.post('/lt',(ctx,next)=>{
  let index=Math.floor(Math.random() * 4) + 0 //0-4
  let {username}=ctx.request.body
  if (username!=='') {
    ctx.body={
      code:200,
      msg:books[index]
    }
  }else{
    ctx.body={
      code:400,
      msg:"不能为空"
    }
  }
  
})




router.post("/login", (ctx, next) => {
  let { username, password } = ctx.request.body;
  let items = user.find((item) => item.username == username && item.password);
  if (items) {
    ctx.body = {
      code: 200,
      msg: "登录成功",
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "登录失败,去注册后重新登录",
    };
  }
});
router.post('/sigin',(ctx,next)=>{
  let {username,password}=ctx.request.body
  let items=user.find((item)=>item.username==username)
  if (items) {
    ctx.body={
      code:400,
      msg:"账号已存在"
    }
  }else{
    user.push({...ctx.request.body})
    ctx.body={
      code:200,
      msg:"注册成功"
    }
  }
})


module.exports = router;
